package cn.kgc.service.impl;

import cn.kgc.entity.Employee;
import cn.kgc.mapper.DepartmentMapper;
import cn.kgc.mapper.EmployeeMapper;
import cn.kgc.service.EmployeeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public Employee login(String empName, String password) {
        Employee query = new Employee(empName,password);    //封装查询条件
        return employeeMapper.selectOne(query);
    }


    @Override
    public PageInfo<Employee> findEmpList(int pageId,int pafeSize){
        //开启分页
        PageHelper.startPage(pageId,pafeSize);
        List<Employee> employees = employeeMapper.selectAll();
        for (Employee employee : employees) {
            employee.setDeptName(departmentMapper.selectByPrimaryKey(employee.getDeptId()).getDepartmentName());
        }
        return  new PageInfo<Employee>(employees);
    }

    @Override
    public boolean addEmployee(Employee employee) {
        employee.setPassword("888888");
        int rows =  employeeMapper.insertSelective(employee);
        //Integer.parseInt("abc");
        return rows > 0;
    }

    @Override
    public Employee findEmployeeId(Integer id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean modifyEmployee(Employee employee) {
        return employeeMapper.updateByPrimaryKeySelective(employee) > 0;
    }

    @Override
    public boolean removeEmployee(Integer id) {
        return employeeMapper.deleteByPrimaryKey(id)>0;
    }
}
